package me.khrystal.cleanmaster.base;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.support.v4.BuildConfig;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/9/14
 * update time:
 * email: 723526676@qq.com
 */
public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static CrashHandler mCrashHandler;
    private Context mContext;

    private CrashHandler() {}

    public static synchronized CrashHandler getInstance() {
        if (mCrashHandler == null) {
            mCrashHandler = new CrashHandler();
        }
        return mCrashHandler;
    }

    public void init(Context context) {
        this.mContext = context;
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

        if (!BuildConfig.DEBUG) {
            StringBuilder sb = new StringBuilder();
            // 1.获取当前应用程序的版本号.
            PackageManager pm = mContext.getPackageManager();
            try {
                PackageInfo packinfo = pm.getPackageInfo(mContext.getPackageName(),
                        0);
                sb.append("程序的版本号为" + packinfo.versionName);
                sb.append("\n");

                // 2.获取手机的硬件信息.
                Field[] fields = Build.class.getDeclaredFields();
                for (int i = 0; i < fields.length; i++) {
                    fields[i].setAccessible(true);
                    String name = fields[i].getName();
                    sb.append(name + " = ");
                    String value = fields[i].get(null).toString();
                    sb.append(value);
                    sb.append("\n");
                }
                // 3.获取程序错误的堆栈信息 .
                StringWriter writer = new StringWriter();
                PrintWriter printWriter = new PrintWriter(writer);
                ex.printStackTrace(printWriter);

                String result = writer.toString();
                sb.append(result);
                String time = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
                String name = "cleanmaster" + time + ".txt";
                String fileName = Environment.getExternalStorageDirectory() + File.separator + "xz_app" + File.separator + "log";

                File file1 = new File(fileName);
                if (!file1.exists()) {
                    file1.mkdir();
                }
                File file = new File(fileName, name);
                if (!file.exists()) {
                    file.createNewFile();
                }
                //    File file = new File(Environment.getExternalStorageDirectory(),"error.log");
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(sb.toString().getBytes());
                fos.close();


                // 4.把错误信息 提交到服务器

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
